import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/mahjong_system_const.dart';
import 'package:freedom/page/mahjong/bloc/user_state.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/export_card.dart';
import 'package:freedom/page/mahjong/mahjong_game.dart';
import 'package:freedom/page/mahjong/provider/mahjong_room_info.dart';
import 'package:freedom/widget/current_game_commons.dart';
import 'package:freedom/widget/mahjong_common.dart';
import 'package:provider/provider.dart';
import 'swap_customer_clip.dart';

typedef CallBack = Function(AnimationController animationController);

class SwapCard extends StatefulWidget {
  final MahjongIndexGame game;
  SwapCard({required this.game});
  @override
  SwapCardState createState() => SwapCardState();
}

class SwapCardState extends State<SwapCard>
    with SingleTickerProviderStateMixin {
  AnimationController? _animationController;
  Animation? _animation;
  bool handClick = false;

  UserState? get currentUser => widget.game.bottomCardArea?.currentUserState;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 500));
    _animation = Tween(begin: 0.0, end: 110.h).animate(_animationController!);
    _animationController!.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<MahjongRoomInfoProvider>(
        builder: (buildContext, provider, child) {
      if (provider.swapCardIn == null || provider.swapCardOut == null) {
        return const SizedBox();
      }
      return Positioned(
        left: 0,
        top: MahjongContext().bImagePoint.y - 80.h,
        child: SafeArea(
            child: Material(
          color: Colors.transparent,
          child: SizedBox(
            width: 110.h,
            height: 80.h,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                InkWell(
                    onTap: () {
                      if (_animationController!.isCompleted) {
                        _animationController!.reverse();
                      } else {
                        _animationController!.forward();
                      }
                    },
                    child: Container(
                        width: 15.h,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Colors.orange,
                            borderRadius: BorderRadius.circular(5.h)),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            tips("换"),
                            tips("牌"),
                            tips("信"),
                            tips("息"),
                          ],
                        ))),
                _animationController!.isAnimating ||
                        _animationController!.isCompleted
                    ? ClipPath(
                        clipBehavior: Clip.antiAliasWithSaveLayer,
                        clipper: SwapCustomerClip(_animation!.value, 80.h),
                        child: Container(
                            width: 90.h,
                            height: 80.h,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.black.withOpacity(0.6),
                                borderRadius: BorderRadius.circular(5.0)),
                            clipBehavior: Clip.antiAliasWithSaveLayer,
                            child: Center(
                              child: Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    changeCard(
                                        "拿进",
                                        provider.swapCardIn
                                                ?.split(",")
                                                .map((e) => int.parse(e))
                                                .toList() ??
                                            []),
                                    changeCard(
                                        "拿出",
                                        provider.swapCardOut
                                                ?.split(",")
                                                .map((e) => int.parse(e))
                                                .toList() ??
                                            []),
                                  ]),
                            )))
                    : const SizedBox()
              ],
            ),
          ),
        )),
      );
    });
  }

  Center tips(String str) {
    return Center(
      child: Text(
        str,
        style: TextStyle(fontSize: 12.h, color: Colors.white, height: 1.0),
      ),
    );
  }

  Widget changeCard(String tips, List<int> cards) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ...cards.map((item) => CardWidget.buildExportCardInfo(item, 30.h))
      ],
    );
  }
}
